package cn.jsu.oj.dispatchServer.satellite.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * 线程配置
 *
 * @author shan
 * @date 2022/06/29
 */
@Slf4j
@Configuration
public class ThreadConfig {

    /**
     * 注册执行线程池bean
     *
     * @return {@link ExecutorService }
     */
    @Bean("executorService")
    ExecutorService initExecuteThreadPool() {
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(true)
                .setNameFormat("thread-call-runner-%d").build();
        LinkedBlockingQueue<Runnable> threadQueue = new LinkedBlockingQueue<>(1024);
        log.info("bean: 执行线程池配置完成......");
        return new ThreadPoolExecutor(2, 7, 
                500L, TimeUnit.MILLISECONDS, 
                threadQueue, threadFactory);
    }

    /**
     * 注册调度线程池bean
     *
     * @return {@link ScheduledExecutorService }
     */
    @Bean("scheduleService")
    ScheduledThreadPoolExecutor initScheduleThreadPool() {
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(true)
                .setNameFormat("thread-call-runner-%d").build();
        ScheduledThreadPoolExecutor scheduleService = new ScheduledThreadPoolExecutor(2, threadFactory);
        log.info("bean: 调度线程池配置完成");
        return scheduleService;
    }
}
